/*
 * GUI_Splash.java
 *
 * Created on 14 de octubre de 2007, 17:37
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package Vista;

/**
 *
 * @author GERBER
 */
import javax.swing.*;
import java.awt.*;

public class GUI_Splash extends JWindow {
    BorderLayout borderLayout1 = new BorderLayout();
    JLabel imageLabel = new JLabel();
    JPanel southPanel = new JPanel();
    FlowLayout southPanelFlowLayout = new FlowLayout();
    JProgressBar progressBar = new JProgressBar();
    ImageIcon imageIcon;

    public GUI_Splash(int duration) {
        try {
            centrarAplicacion();
            contenido();
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
        cargar(duration);
    }


    public void centrarAplicacion() {
        // Set the window's bounds, centering the window
        int width = 450;
        int height =250;
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (screen.width-width)/2;
        int y = (screen.height-height)/2;
        setBounds(x,y,width,height);
    }

    void contenido() throws Exception {
        JPanel content = (JPanel)getContentPane();
        content.setBackground(Color.white);

        // Build the splash screen
        JLabel logo = new javax.swing.JLabel(
                new ImageIcon(getClass().getResource("/Archivos/Imagenes/gifSplash.gif")),JLabel.CENTER);

        content.add(logo, BorderLayout.CENTER);
        southPanel.setLayout(southPanelFlowLayout);
        southPanel.setBackground(Color.BLACK);
        content.add(southPanel, BorderLayout.SOUTH);
        southPanel.add(progressBar, null);

        Color blue = new Color(102,153,255);
        content.setBorder(BorderFactory.createLineBorder(blue, 3));
    }

    public void setProgressMax(int maxProgress) {
        progressBar.setMaximum(maxProgress);
    }

    public void setProgress(int progress) {
        final int theProgress = progress;
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                progressBar.setValue(theProgress);
            }
        });
    }

    public void setProgress(String message, int progress) {
        final int theProgress = progress;
        final String theMessage = message;
        setProgress(progress);
        setVisible(true);
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                progressBar.setValue(theProgress);
                setMessage(theMessage);
            }
        });
    }

    public void setScreenVisible(boolean b) {
        final boolean boo = b;
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                setVisible(boo);
            }
        });
    }

    private void setMessage(String message) {
        if (message==null) {
            message = "";
            progressBar.setStringPainted(false);
        }
        else {
            progressBar.setStringPainted(true);
        }
            progressBar.setString(message);
    }

    private void cargar(int duration) {
        for (int i = 0; i <= 100; i++) {
            for (long j=0; j<10; ++j) {
                setVisible(true);
                try {
                    Thread.sleep(duration);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
            setProgress("Cargando " + i, i);
        }
        dispose();
    }
}

